package com.jilian.mydemo.modul.order.ui;

import android.app.Dialog;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.OrderDetailDto;
import com.jilian.mydemo.common.vo.ProductVO;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.ProductAdapter;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.PinziDialogUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.views.CustomLinearLayoutManager;
import com.jilian.mydemo.views.CustomerItemDecoration;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class OneOrderDetailActivity extends BaseOrderDetailActivity implements CustomItemClickListener {
    private TextView tvOrderLevel;
    private TextView tvOrderType;
    private TextView tvBillingType;



    private TextView tvAdress;
    private TextView tvOrderNo;

    private TextView tvAppointmentTime;
    private TextView etContent;
    private TextView etDemandContent;
    private TextView tvAccessoriesFee;
    private TextView tvServiceFee;
    private TextView tvPreferential;
    private TextView tvThisCard;
    private TextView tvOk;
    private RecyclerView recyclerView;
    private CustomLinearLayoutManager linearLayoutManager;
    private List<ProductVO> datas;
    private ProductAdapter adapter;
    private TextView tvServiceType;
    private TextView tvEqCount;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }


    @Override
    public int intiLayout() {
        return R.layout.activity_oneorderdetail;
    }

    @Override
    public void initView() {
        setNormalTitle("订单详情", v -> finish());
        tvOrderLevel = (TextView) findViewById(R.id.tv_order_level);
        tvOrderType = (TextView) findViewById(R.id.tv_order_type);
        tvBillingType = (TextView) findViewById(R.id.tv_billing_type);
        tvAdress = (TextView) findViewById(R.id.tv_adress);
        tvOrderNo = (TextView) findViewById(R.id.tv_order_no);
        tvAppointmentTime = (TextView) findViewById(R.id.tv_appointment_time);
        etContent = (TextView) findViewById(R.id.et_content);
        etDemandContent = (TextView) findViewById(R.id.et_demand_content);
        tvAccessoriesFee = (TextView) findViewById(R.id.tv_accessories_fee);
        tvServiceFee = (TextView) findViewById(R.id.tv_service_fee);
        tvPreferential = (TextView) findViewById(R.id.tv_preferential);
        tvThisCard = (TextView) findViewById(R.id.tv_this_card);
        tvServiceType = (TextView) findViewById(R.id.tv_service_type);
        tvEqCount = (TextView) findViewById(R.id.tv_eq_count);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.addItemDecoration(new CustomerItemDecoration(DisplayUtil.dip2px(this, 10)));
        linearLayoutManager = new CustomLinearLayoutManager(this);
        linearLayoutManager.setScrollEnabled(false);
        recyclerView.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        adapter = new ProductAdapter(this, datas, this);
        recyclerView.setAdapter(adapter);
    }

    private OrderDetailDto orderDetailDto;
    @Override
    protected void initDetailView(OrderDetailDto data) {
        this.orderDetailDto = data;
        if ("1".equals(data.getIsMore())) {
            setrightImageOne(R.drawable.image_nav_icon_group, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(OneOrderDetailActivity.this, HydraulicListActivity.class);
                    intent.putExtra("id", orderDto.getId());
                    intent.putExtra("orderDetailDto", orderDetailDto);
                    startActivity(intent);
                }
            });
        }
        //工单紧急度
        tvOrderLevel.setText(data.getOrderLevel());
        //工单类型
        tvOrderType.setText(data.getOrderType());
        //计件类型
        tvBillingType.setText(data.getBillingType());
        //地址
        tvAdress.setText(data.getServiceAddress());
        //订单号
        tvOrderNo.setText(data.getOrderNo());
        //服务类型
       tvServiceType.setText(data.getServiceType());
        //设备数量
       tvEqCount.setText(data.getDeviceAmount() + "台");
        //服務產品 為列表
        datas.clear();
        datas.addAll(data.getProductVOS());
        adapter.notifyDataSetChanged();
        //预约时间
        tvAppointmentTime.setText(data.getBookTime());
        //语预约说明
        etContent.setText(data.getBookRemark());
        //服务需求
        etDemandContent.setText(data.getServiceRemark());
        //配件费 保留两位
        tvAccessoriesFee.setText(data.getPartsMoney() == null ? "0.00" : data.getPartsMoney().setScale(2, BigDecimal.ROUND_DOWN).toString());
        //服务费
        tvServiceFee.setText(data.getServiceMoney() == null ? "0.00" : data.getServiceMoney().setScale(2, BigDecimal.ROUND_DOWN).toString());
        //优惠
        tvPreferential.setText(data.getReduceMoney());
        //挂账
        tvThisCard.setText(data.getPendingMoney() == null ? "0.00" : data.getPendingMoney().setScale(2, BigDecimal.ROUND_DOWN).toString());
    }


    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!("1".equals(orderDetailDto.getIsLeader()))&&"1".equals(orderDetailDto.getIsMore())) {
                    ToastUitl.showImageToastTips("只有队长可以操作");
                    return;
                }
                if (EmptyUtils.isNotEmpty(orderDto)) {
                    showTipsDialog(orderDto.getId());
                }

            }
        });
    }

    /**
     * 提示对话框
     */
    private void showTipsDialog(String id) {

        Dialog dialog = PinziDialogUtils.getDialogNotTouchOutside(this, R.layout.dialog_confirm);
        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tvContent = (TextView) dialog.findViewById(R.id.tv_content);
        tvTitle.setText("抢单确认");
        tvContent.setText("确定抢单吗？");
        TextView tvNo = (TextView) dialog.findViewById(R.id.tv_no);
        TextView tvOk = (TextView) dialog.findViewById(R.id.tv_ok);

        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                grabOrder(id);
            }
        });
        tvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();


    }

    /**
     * @param id 订单ID
     */
    private void grabOrder(String id) {
        showLoadingDialog();
        orderViewModel.grabOrder(id);
        orderViewModel.getGrabliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if (dto.isSuccess()) {
                    ToastUitl.showImageToastSuccess("抢单成功");
                    finish();
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                }

            }
        });

    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
